<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="产品名称" prop="coalName">
        <el-input
          clearable
          :maxlength="50"
          v-model="queryParams.coalName"
          placeholder="请输入产品名称"
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button type="primary" plain @click="createFn" v-hasPermi="['bid:product:create']">
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      stripe
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
    >
      <el-table-column label="编号" align="center" width="60">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="产品名称"
        align="center"
        prop="coalName"
        width="250"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        min-width="300"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="创建人"
        align="center"
        prop="createName"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="200"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center" width="110" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="updateFn(scope.row.id)"
            v-hasPermi="['bid:product:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            @click="deleteFn(scope.row.id)"
            v-hasPermi="['bid:pruduct:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <productForm ref="productFormRef" @success="success"></productForm>
</template>
<script setup lang="ts">
import productForm from './productForm.vue'
import { dateFormatter } from '@/utils/formatTime'
import * as production from '@/api/production'
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const message = useMessage()
const productFormRef = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  coalName: undefined
})
const queryFormRef = ref()
const emit = defineEmits(['subPage'])

/** 查询列表 */

const getList = async () => {
  loading.value = true
  try {
    const data = await production.getList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const createFn = () => {
  productFormRef.value.open()
}

const updateFn = (id: number) => {
  productFormRef.value.open(id)
}

const deleteFn = async (id: number) => {
  await message.delConfirm()
  const res = await production.coalManageDelete(id)
  if (res) {
    message.success('操作成功')
    success()
  } else {
    message.success('操作失败')
  }
}

const success = () => {
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
